import { requestSystemClient } from '#/api/request';

export namespace SystemDeptApi {
  export interface SystemDept {
    [key: string]: any;
    children?: SystemDept[];
    deptId: number;
    deptName: string;
    parentId: number;
    showSort: number;
    deptStatus: 0 | 1;
    leaderUserId: number;
    deptPhone: string;
    deptEmail: string;
    label: string; // 节点显示文本
    id: number; // 节点显示id
  }
}

const rootDept: SystemDeptApi.SystemDept = {
  deptName: '所有部门',
  children: [],
  deptId: 0,
  parentId: -1,
  showSort: 0,
  deptStatus: 1,
  leaderUserId: 0,
  deptPhone: '',
  deptEmail: '',
  label: '所有部门', // 节点显示文本
  id: 0, // 节点显示id
};
/**
 * 获取部门列表数据
 */
async function getDeptList() {
  const data =
    requestSystemClient.get<Array<SystemDeptApi.SystemDept>>(
      '/system/dept/list',
    );
  return data;
}

/**
 * 创建部门
 * @param data 部门数据
 */
async function createDept(
  data: Omit<SystemDeptApi.SystemDept, 'children' | 'deptId'>,
) {
  return requestSystemClient.post('/system/dept/create', data);
}

/**
 * 更新部门
 *
 * @param id 部门 ID
 * @param data 部门数据
 */
async function updateDept(
  deptId: number,
  data: Omit<SystemDeptApi.SystemDept, 'children' | 'deptId'>,
) {
  data.deptId = deptId;
  return requestSystemClient.put(`/system/dept/update`, data);
}

/**
 * 删除部门
 * @param id 部门 ID
 */
async function deleteDept(deptId: number) {
  return requestSystemClient.delete(`/system/dept/delete/${deptId}`);
}

export { createDept, deleteDept, getDeptList, rootDept, updateDept };
